from device_list import devicelist
from get_status_xml import *
# from qyt_netconf_connect import netconf_connect
import xml.etree.ElementTree as ET
import sys

#定义连接netconf的函数
def netconf_connect(host_ip,params,
                    port=830, user='qytang', password='Dashu666!'):
    return manager.connect(host=host_ip,
                       port=port,
                       username=user,
                       password=password,
                       hostkey_verify = False,
                       device_params={'name':params},
                       allow_agent = False,
                       look_for_keys = False)

for i in range(len(devicelist)):  #使用for循环遍历设备信息列表
    device_name = devicelist[i]['name']  #实例化设备名字
    host_ip = devicelist[i]['ip']  #实例化设备ip地址
    params = devicelist[i]['params']  #实例化设备平台参数
    m = netconf_connect(host_ip, params)    #实例化netconf连接
    try:
        if params == 'csr':   #使用判断语句判断设备平台
            get_status = get_status1
            print(type(get_status))
            # peer_rid_tag = '{http://cisco.com/ns/yang/Cisco-IOS-XE-ospf-oper}nbr-id'
        elif params == 'huawei':
            get_status = get_status0
            print(type(get_status))
            # peer_rid_tag = '{http://www.huawei.com/netconf/vrp}nbrRouterId'
    except:
        print(device_name + 'is not csr or huawei device')
        pass

    # status = m.get(get_status)  #获取设备OSPF邻居状态信息
    # print('=' * 80)
    # print(type(status))

    # 将xml编码的数据解读为 'lxml.etree._Element'
    # status = m.get(get_status).data
    # print('=' * 80)
    # print(type(status))

    # 根据标签取值
    # status = m.get(get_status).data
    # tag_text = status.findtext(".//" + peer_rid_tag)

    #通过判断语句，将不是点分十进制的IP地址进行转换
    # if tag_text.find('.') == -1:  #如果在返回结果中查找不到“.”则将其转换为点分十进制
    #     PEER_RID=ip10_to_ip(int(tag_text)) #调用函数来完成IP格式转换操作
    # else:
    #     PEER_RID=tag_text
    # print(PEER_RID)

    # 从当前 etree 中遍历标签
    # for i in status.iter():
    #     print(i.tag)

